/*
 * ml20gui - The MSN Webcam Recorder
 * This source code is under the GNU Public License (GPL).
 * Written by Ramiro Polla
 * Initial release: june 3rd, 2005
 *
 * this program records MSN Webcam Video
 */

#include "ml20gui.h"

LRESULT CALLBACK AboutProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg) {
    case WM_INITDIALOG:
        {
            char appversion[MAX_PATH];
            sprintf( appversion, "%s %s - GPL", appname, ml20dll_version( ) );
            SetWindowText( hwnd, str_about );
            SetDlgItemText( hwnd, IDC_APPNAME, appversion );
            SetDlgItemText( hwnd, IDC_WRITTEN, str_written );
            SetDlgItemText( hwnd, IDC_TRANSLATED, str_translated );
            SetDlgItemText( hwnd, IDC_MICROSOFT, str_webcam_icon );
            SetDlgItemText( hwnd, IDC_ABOUT3, str_about1 );
            SetDlgItemText( hwnd, IDC_ABOUT2, str_about2 );
            SetDlgItemText( hwnd, IDC_CLOSE, str_close );
        }
        break;
    case WM_CLOSE:
        EndDialog( hwnd, 0 );
        break;
    case WM_COMMAND:
        {
            switch(LOWORD(wParam)) {
            case IDC_HTTP:
                ShellExecute( NULL, NULL, getbrowser( ), "http://ml20rc.msnfanatic.com", NULL, SW_SHOWNORMAL );
                break;
            case IDC_CLOSE:
                EndDialog( hwnd, 0 );
                break;
            }
            break;
        }
    default:
        return FALSE;
    }
    return TRUE;
}

static char browser[MAX_PATH];

char *getbrowser( )
{
    HKEY hKey = NULL;
    char *ret = browser;
    ret[0] = 0;

    // Open the registry
    if (RegOpenKeyEx(HKEY_CLASSES_ROOT, "http\\shell\\open\\command",
        0, KEY_READ, &hKey) == ERROR_SUCCESS)
    {
        // Data size
        DWORD cbData = 0;
        // Get the default value
        if (RegQueryValueEx(hKey, NULL, NULL, NULL, NULL, &cbData) == ERROR_SUCCESS && cbData > 0) {
            // Allocate a suitable buffer
            char* psz = malloc( MAX_PATH );
            // Success?
            if (psz != NULL) {
                if (RegQueryValueEx(hKey, NULL, NULL,
                    NULL, (LPBYTE)psz, &cbData) ==
                    ERROR_SUCCESS)
                {
                    // Success!
                    memcpy( ret, psz, strlen(psz) + 1 );
                }
                free( psz );
            }
        }
        RegCloseKey(hKey);
    }

    // Do we have the browser?
    if ( ret[0] ) {
        int istart;
        int iend;
        int size = strlen( ret );
        int i;

        for( istart = 0 ; ret[istart] != '"' && istart < size ; istart++ );
        for( iend = size ; ret[iend] != '"' && iend != 0 ; iend-- );

        if ( istart <= size && iend >= 0) {
            if ( (iend - istart) <= 3 ) {
                for( i = 0 ; ret[i] != '\0' ; i++ )
                    ret[i] = toupper( ret[i] );
                for( i = 0 ; i < (size-4) && memcmp(ret+i, ".EXE", 4) ; i++ );
                ret[i+4] = '\0';
            } else if (istart != iend) {
                ret[iend] = '\0';
                ret += istart + 1;
            }
        }
        return ret;
    }
    return 0;
}
